package etc.helper;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;


@SuppressWarnings("javadoc")
public class IniFile
{

	public static final String DEFAULT_NODE = "Default";

	public static HashMap<String, String> load(String path) throws IOException
	{
		HashMap<String, String> result = new HashMap<String, String>();

		FileInputStream fis = new FileInputStream(path);
		DataInputStream dis = new DataInputStream(fis);
		BufferedReader br = new BufferedReader(new InputStreamReader(dis));

		String currentNode = DEFAULT_NODE;
		String line;
		while ((line = br.readLine()) != null)
		{
			// Remove comments
			int commentIndex = line.indexOf(";");
			if(commentIndex >= 0)
			{
				line = line.substring(0, commentIndex);
			}

			// Remove whitespace
			line = line.trim();

			// Check whether the line is empty
			if(line.equals(""))
			{
				continue;
			}

			// Check whether its a node
			if(line.matches("\\[[.\\w]+\\]"))
			{
				currentNode = line.substring(1, line.length() - 1);
				continue;
			}

			// Split the line to get trimmed key and value
			String[] lineSplit = line.split("=", 2);
			String key = currentNode+"."+lineSplit[0].trim();
			String value = lineSplit[1].trim();

			// Check whether a value exists
			if(value.length() > 0)
			{
				// Insert the data
				result.put(key, value);
			}
		}

		// Return the result
		return result;
	}

	public static void save(@SuppressWarnings("unused") String path, HashMap<String, String> data)
	{
		String result = "";

		// Get the iterator to iterate entries
		Iterator<Entry<String, String>> iterator = data.entrySet().iterator();

		// Create key and value caches
		String[] keys = new String[data.size()];
		String[] values = new String[data.size()];
		
		// Iterate all entries by reverse indexes
		for(int i = data.size() - 1; iterator.hasNext(); i--)
		{
			// Get the entries and the key and value of it and store to the caches
			Entry<String, String> entry = iterator.next();
			keys[i] = entry.getKey();
			values[i] = entry.getValue();
		}

		String currentNode = DEFAULT_NODE;
		boolean firstNode = true;
		for(int i = 0; i < keys.length; i++)
		{
			// Set some data
			String[] keySplit = keys[i].split("\\.", 2);
			String node = keySplit[0];
			String key = keySplit[1];
			
			// Check whether the current node is not the last node
			if(!node.equals(currentNode))
			{
				// Check whether we are in the first node
				if(!firstNode)
				{
					// Append a newline
					result += "\n";
				}
				else
				{
					firstNode = false;
				}

				// Set the next current node
				currentNode = node;

				// Append the node
				result += "["+node+"]\n";
			}

			// Append the key and value
			result += key + " = " + values[i] + "\n";
		}
		System.out.println(result);
	}

}
